package com.example.demo.controller;


import com.example.demo.common.BaseResponse;
import com.example.demo.common.ResultUtils;
import com.example.demo.myModel.entity.Email;
import com.example.demo.myModel.entity.Teacher;
import com.example.demo.myModel.otd.user.AdminLoginRequest;
import com.example.demo.myModel.otd.user.SentEmailRequest;
import com.example.demo.myModel.otd.user.TeacherRegisterRequest;
import com.example.demo.service.My.TeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;


/***
 * 管理员（超级管理员与赛事管理员）操作
 */
@RestController
@RequestMapping("/teacher")
@Slf4j
public class TeacherController {

    @Resource
    private TeacherService teacherService;



    /**
     * 发送注册码给邮箱
     *
     * @param sentEmailRequest 请求参数{
     *                         "userId":12553315621
     *                         "email":2222@qq.com
     *                         }
     * @return {
     * "code":200,
     * "message":null,
     * "data":265656
     * }
     */
    @PostMapping("/sent/Email")
    public BaseResponse<Boolean> sentEmail(@RequestBody SentEmailRequest sentEmailRequest, HttpSession session) {
        Email email = new Email();
        email.setEmail(sentEmailRequest.getEmail());
        email.setUserId(sentEmailRequest.getUserId());
        boolean b =teacherService.sentEmailCode(email, session);
        return ResultUtils.success(b);
    }

    /**
     * 管理员注册
     * @param teacherRegisterRequest
     * @param session
     * @return
     */
    @PostMapping("/admin/Register")
    public BaseResponse<Long> adminRegister(@RequestBody TeacherRegisterRequest teacherRegisterRequest, HttpSession session){
        Teacher teacher = new Teacher();
        teacher.setTeacherName(teacherRegisterRequest.getTeacherName());
        teacher.setTeacherId(teacherRegisterRequest.getTeacherId());
        teacher.setPassword(teacherRegisterRequest.getPassword());
        teacher.setEmail(teacherRegisterRequest.getEmail());
        teacher.setRoleId(teacherRegisterRequest.getRoleId());
        teacher.setCode(teacherRegisterRequest.getCode());
        teacher.setCollege(teacherRegisterRequest.getCollege());
        teacher.setSchoolName(teacherRegisterRequest.getSchoolName());
        teacher.setTel(teacherRegisterRequest.getTel());
        teacher.setCreateTime(LocalDateTime.now());
        teacher.setGender(teacherRegisterRequest.getGender());
        teacher.setState(1);
        long id = teacherService.adminRegister(teacher, session);
        return  ResultUtils.success(id);
    }



    /**
     * 管理员登录
     * @param adminLoginRequest 登录请求类
     * @param request 请求信息
     * @return
     * {
     *     true
     * }
     */
    @PostMapping("/adminLogin")
    public BaseResponse<Boolean> adminLogin(@RequestBody AdminLoginRequest adminLoginRequest, HttpServletRequest request) {
        teacherService.adminLogin(adminLoginRequest,request);
        return ResultUtils.success(true);
    }



}
